using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Authorization;
using JGSY.CMS.LowCode.Platform.Application.Interfaces;
using JGSY.CMS.LowCode.Platform.Application.DTOs;
using System.Threading.Tasks;

namespace JGSY.CMS.LowCode.Platform.Controllers
{
    /// <summary>
    /// 社交媒体认证管理接口。
    /// 提供第三方社交媒体登录、绑定、解绑等相关功能的 API。
    /// </summary>
    [ApiController]
    [Route("api/auth/social")]
    public class SocialAuthController : ControllerBase
    {
        /// <summary>
        /// 社交媒体认证应用服务接口
        /// </summary>
        private readonly ISocialAuthAppService _service;
        
        /// <summary>
        /// 构造函数，注入社交媒体认证服务
        /// </summary>
        /// <param name="service">社交媒体认证应用服务接口</param>
        public SocialAuthController(ISocialAuthAppService service)
        {
            _service = service;
        }

        /// <summary>
        /// 微博社交登录
        /// </summary>
        /// <param name="code">授权码</param>
        /// <returns>登录结果</returns>
        [HttpPost("weibo")]
        public async Task<IActionResult> Weibo([FromBody] string code)
        {
            return Ok(await _service.LoginWithWeiboAsync(code));
        }

        /// <summary>
        /// 钉钉社交登录
        /// </summary>
        /// <param name="code">授权码</param>
        /// <returns>登录结果</returns>
        [HttpPost("dingtalk")]
        public async Task<IActionResult> DingTalk([FromBody] string code)
        {
            return Ok(await _service.LoginWithDingTalkAsync(code));
        }

        /// <summary>
        /// 获取社交媒体配置信息
        /// </summary>
        /// <returns>社交媒体配置信息</returns>
        [HttpGet("config")]
        public async Task<IActionResult> GetSocialConfig()
            => Ok(await _service.GetSocialConfigAsync());

        /// <summary>
        /// Github OAuth 回调处理
        /// </summary>
        /// <param name="code">授权码</param>
        /// <param name="state">状态参数</param>
        /// <returns>回调处理结果</returns>
        [HttpGet("github/callback")]
        public async Task<IActionResult> GithubCallback(string code, string state)
            => Ok(await _service.GithubCallbackAsync(code, state));

        /// <summary>
        /// Google OAuth 回调处理
        /// </summary>
        /// <param name="code">授权码</param>
        /// <param name="state">状态参数</param>
        /// <returns>回调处理结果</returns>
        [HttpGet("google/callback")]
        public async Task<IActionResult> GoogleCallback(string code, string state)
            => Ok(await _service.GoogleCallbackAsync(code, state));

        /// <summary>
        /// 绑定社交媒体账户
        /// </summary>
        /// <param name="dto">社交媒体绑定请求数据传输对象</param>
        /// <returns>绑定结果</returns>
        [Authorize]
        [HttpPost("bind")]
        public async Task<IActionResult> Bind([FromBody] SocialBindRequestDto dto)
        {
            var result = await _service.BindAsync(dto.Provider, dto.Code);
            return Ok(result);
        }

        /// <summary>
        /// 解除社交媒体账户绑定
        /// </summary>
        /// <param name="provider">社交媒体提供商</param>
        /// <returns>解绑结果</returns>
        [Authorize]
        [HttpDelete("unbind/{provider}")]
        public async Task<IActionResult> Unbind(string provider)
        {
            var result = await _service.UnbindAsync(provider);
            return Ok(result);
        }
    }
}
